#include <rtdef.h>
#include <rtthread.h>
#include "power.h"
#include "MQ.h"
#include "adc.h"

rt_adc_device_t ADCdev;
        
static void timerPowerOut(void * parameter)
{
    //发送读取到的电压值
    Queuemsg.id =1;
    Queuemsg.data = rt_adc_read(ADCdev, ADC_SYSTEM_CHANNEL);
    
    Queuemsg.data =  Queuemsg.data * REFER_VOLTAGE / CONVERT_BITS * REAL_VOLTAGE;
    rt_mq_send(mqLed_handle, &Queuemsg, sizeof(Queuemsg));
    
    Queuemsg.data = rt_adc_read(ADCdev, ADC_CLOCK_CHANNEL);
    Queuemsg.data =  Queuemsg.data * REFER_VOLTAGE / CONVERT_BITS * 2;
}

static void power_timer_init()
{
    rt_timer_t  timerPower;
    timerPower = rt_timer_create( "timerPower", timerPowerOut, RT_NULL, 50, RT_TIMER_FLAG_PERIODIC);
    rt_timer_start(timerPower);
}
void power_thread_entry(void *parameter)
{
    power_timer_init();
    
    ADCdev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
    if (ADCdev)
    {
        /* 打开设备 */
        rt_adc_enable(ADCdev, ADC_SYSTEM_CHANNEL);
        rt_adc_enable(ADCdev, ADC_CLOCK_CHANNEL);
    
    }

    while(1)
    {
        rt_thread_mdelay(100);
    }
    
}